package com.example.demo2.demo01;

import com.example.demo2.rabbitmq.RabbitMQConnection;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description: 消费者 - 公平队列 - 自动应答(autoAck= true)
 * 每个消费者消费的数量一致  不管消费速度如何  属于均摊消费 模拟消费消息没有延迟
 * @Author: mingtian
 * @CreateDate: 2021/1/16 09:20
 * @Version: 1.0
 */
public class Consumer02 {
    /**
     * 队列名称
     */
    private static final String QUEUE_NAME = "test_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.创建连接
        Connection connection = RabbitMQConnection.getConnection();
        // 2.设置通道
        Channel channel = connection.createChannel();
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body, "UTF-8");
                System.out.println("消费者获取消息:" + msg);
            }
        };
        // 3.监听队列  autoAck true:自动应答  false:手动应答
        channel.basicConsume(QUEUE_NAME, true, defaultConsumer);

    }
}